﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV.CvEnum;
using finProj.Entities;
using MatrixLibrary;

namespace finProj.Gui
{
    public partial class Form2 : Form
    {
        
        private int clusterid;
        private string filename;
        private bool cleared = false;

        public Form2()
        {
            InitializeComponent();
            if (Settings.Instance.state == 1)
            {

                openImageToolStripMenuItem.Enabled = false;
                loadDatasetToolStripMenuItem.Enabled = true;

            }
        }

        private void BrowseButton1_Click(object sender, EventArgs e)
        {
            string path;
            var openFiledlg = new OpenFileDialog();
            openFiledlg.Filter = "Images (*.jpg, *.png, *.bmp) |*.jpg;*.png;*.bmp";
            openFiledlg.Multiselect = false;
            DialogResult res = openFiledlg.ShowDialog();

            if (res == DialogResult.OK)
            {
                //string filename = openFiledlg.FileName;
                path = openFiledlg.FileName; 
                PathBox.Text = path;
                filename = path;
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            var form = new Form2();
            form.Show();
            cleared = true;

            this.Close();
            this.Dispose();
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            if (PathBox.Text.Length > 0)
            {
                var pic = new Picture(filename);
                pic.detectFace();
                if (! pic.detectFeatures())
                {
                    MessageBox.Show("This image is corrupted or features cannot be detected! Please choose another image.",
                     "Corrupt image", MessageBoxButtons.OK);

                }
                else
                {
                    var kmeans = new KMeans();
                    double[] temp = new double[Settings.Instance.existingKeys.Count];
                    //for (int j = 0; j < Settings.Instance.existingKeys.Count; j++)
                    //{

                    //    temp[j] = pic.FeaturesVec.features[Settings.Instance.existingKeys[j]];

                    //}
                    //temp = Matrix.TwoD_2_OneD(Matrix.Multiply((Settings.Instance.MinimizationMatrix), (Matrix.OneD_2_TwoD(temp))));
                    //pic.FeaturesVec.Change_featurevectors_keys(temp);
                    clusterid = kmeans.findClusterNEW(pic);

                    Settings.Instance.clusterManager.clusters[clusterid].pictures.Add(pic);


                     /********************************
                 * using minimizzation matrix, find corresponding cluster
                 * 
                 * 
                 * add picture to the cluster
                 * add picture to the pictures list
                 * 
                 */


                // -----------------------------------------------
                // i is the cluster id where we found the person
                pictureExample.Image =
                    Settings.Instance.clusterManager.clusters[clusterid].pictures[0].origPicture.Resize(150, 150,
                                                                                                INTER.CV_INTER_LINEAR).
                        ToBitmap();
                Namelbl.Text = Settings.Instance.clusterManager.clusters[clusterid].Name;


                picturebelongslbl.Visible = true;
                Namelbl.Visible = true;
                pictureExample.Visible = true;
                morebtn.Visible = true;
                GoButton.Visible = false;
                BrowseButton1.Enabled = false;
                ClearButton.Visible = true;
                this.Refresh();
                }
               


            }
        }

        private void morebtn_Click(object sender, EventArgs e)
        {
            /*
             *  Get clusterid from the results..
             * 
             * 
             * **/


            var clusterPrev = new clusterPreview(clusterid);
            clusterPrev.Show();
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void clustersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var clsedit = new clustersEdit();
            clsedit.Show();
        }

        void Form2_Closed(object sender, System.EventArgs e)
        {
            if (!cleared)
            {
                this.Dispose();
                Application.Exit();
            }
            
        }

        private void openHelpDocumentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help.Instance.Show();
        }
    }
}
